package com.jiaoshouMr.ethereum.repository;

import com.jiaoshouMr.ethereum.domain.GhostBlockAccount;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.Map;

import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query.query;

@Repository
public class EthRepository {

    @Autowired
    private MongoOperations mongoOperations;

    public void bindEthAccount(GhostBlockAccount ghostBlockAccount) {
        mongoOperations.insert(ghostBlockAccount, "ghostBlockAccount");
    }

    public GhostBlockAccount getAccount(String ghostId,String type) {
        Query query = query(where("ghostId").is(ghostId).and("type").is(type));
        return mongoOperations.findOne(query, GhostBlockAccount.class, "ghostBlockAccount");
    }

    public Map findGhost(String ghostId) {
        Query query = query(where("_id").is(new ObjectId(ghostId)));
        return mongoOperations.findOne(query, Map.class , "ghost");
    }
}
